﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Internaltools2
    {
        public static void _Internaltools2(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);


            double scale = 1; //放大系数

            double l1 = Convert.ToDouble(data["lf"]) * scale;
            double h = Convert.ToDouble(data["h"]) * scale;
            double b = Convert.ToDouble(data["b"]) * scale;
            double l2 = Convert.ToDouble(data["ldred"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double n = Convert.ToDouble(data["wf2"]) * scale;
            double d = Convert.ToDouble(data["dcon"]) * scale;
            double D = Convert.ToDouble(data["dmin"]) * scale;

            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X - b / 2, v1.Y);
            Vector2 v3 = new Vector2(v2.X, v2.Y - b);
            Vector2 v4 = new Vector2(v1.X, v3.Y);
            Vector2 v5 = new Vector2(v4.X + 2, v4.Y);
            Vector2 v6 = new Vector2(v5.X + b, v5.Y);
            Vector2 v7 = new Vector2(v6.X, v1.Y);
            Vector2 v8 = new Vector2(v5.X, v1.Y);
            Vector2 v9 = new Vector2(v1.X, v1.Y - b / 2 * b / (v7.X - v2.X));
            Vector2 v10 = new Vector2(v8.X, v8.Y - (v8.X - v2.X) * b / (v7.X - v2.X));
            Vector2 v11 = new Vector2(v4.X, v4.Y + b / 2 * b / (v7.X - v2.X));
            Vector2 v12 = new Vector2(v5.X, v5.Y + (v8.X - v2.X) * b / (v7.X - v2.X));
            Vector2 v13 = new Vector2(v3.X - l2, v3.Y - n);
            Vector2 v14 = fun.Polar(v13, 10 * pi / 180, l);
            Vector2 v15 = fun.Polar(v14, 100 * pi / 180, 1);
            Vector2 v16 = new Vector2(v14.X + l2 / 4, v3.Y);
            Vector2 v17 = fun.Polar(v13, 85 * pi / 180, b / 2.4);
            Vector2 v18 = fun.Polar(v17, 8 * pi / 180, l / 4);
            Vector2 v19 = fun.Polar(v17, 8 * pi / 180, l / 8);
            Vector2 v20 = new Vector2(v19.X + 0.8, v19.Y + b / 1.7);
            Vector2 v21 = new Vector2(v16.X, v2.Y);
            Vector2 v22 = new Vector2(v20.X + l2 / 5, v20.Y + (v21.Y - v20.Y) * (l2 / 5 / (v21.X - v20.X)));
            Vector2 v23 = new Vector2(v22.X + l / 3, v15.Y + (v16.Y - v15.Y) * (v22.X - v15.X + l / 3) / (v16.X - v15.X));
            Vector2 v24 = new Vector2(v1.X, v1.Y + 3);
            Vector2 v25 = new Vector2(v4.X, v4.Y - 3);
            Vector2 v26 = new Vector2(v8.X, v8.Y + 3);
            Vector2 v27 = new Vector2(v5.X, v5.Y - 3);

            Vector2 v28 = new Vector2(v7.X + l2, v7.Y - b / 2);
            Vector2 v29 = new Vector2(v28.X - l2 / 3, v28.Y);
            Vector2 v30 = new Vector2(v28.X + l2 / 3, v28.Y);
            Vector2 v31 = new Vector2(v28.X, v28.Y + l2 / 3);
            Vector2 v32 = new Vector2(v28.X, v28.Y - l2 / 3);
            Vector2 v33 = new Vector2(v28.X + h / 5.3, v6.Y);
            Vector2 v34 = new Vector2(v33.X, v7.Y);
            Vector2 v35 = new Vector2(v34.X, v34.Y + b / 4.48);
            Vector2 v36 = new Vector2(v28.X, v35.Y);
            Vector2 v37 = new Vector2(v28.X, v34.Y);
            Vector2 v38 = new Vector2(v34.X - h, v34.Y);
            Vector2 v39 = new Vector2(v38.X, v6.Y);
            Vector2 v40 = new Vector2(v28.X, v35.Y - D);
            Vector2 v41 = fun.Polar(v40, 100 * pi / 180, b * 1.35);
            Vector2 v42 = new Vector2(v33.X - h / 7.12, v33.Y);
            Vector2 v43 = fun.Polar(v40, 100 * pi / 180, b/ 2.11);
            Vector2 v44 = new Vector2(v43.X - h / 5.8, v43.Y);
            Vector2 v45 = fun.Polar(v44, -82 * pi / 180, b / 2.64);
            Vector2 v46 = fun.Polar(v44, -82 * pi / 180, b / 3.34);
            Vector2 v47 = new Vector2(v46.X - h / 8.38, v46.Y + 0.15);
            Vector2 v48 = new Vector2(v39.X, v39.Y + b / 5.66);
            Vector2 v49 = new Vector2(v39.X + (v47.X - v48.X) * (b / 5.66) / (v48.Y - v47.Y), v39.Y);
            Vector2 v50 = new Vector2(v39.X, v28.Y);
            Vector2 v51 = fun.Polar(v40, 100 * pi / 180, b * 1.18);
            Vector2 v52 = new Vector2(v50.X, v50.Y + b / 7);
            Vector2 v53 = new Vector2(v33.X, v28.Y + b / 6.65);
            



            //作图
            Line line1 = new Line(v24, v25);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line2 = new Line(v26, v27);
            line2.Layer = centre;
            line2.Linetype = Linetype.DashDot;
            line2.LinetypeScale = 3;
            dxf.AddEntity(line2);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v1));
            line3.Vertexes.Add(new LwPolylineVertex(v2));
            line3.Vertexes.Add(new LwPolylineVertex(v21));
            line3.Vertexes.Add(new LwPolylineVertex(v22));
            line3.Vertexes.Add(new LwPolylineVertex(v20));
            line3.Vertexes.Add(new LwPolylineVertex(v19));
            fun.PolyFill(dxf, line3, 0.3, contour);

            LwPolyline line4 = new LwPolyline();
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes.Add(new LwPolylineVertex(v14));
            line4.Vertexes.Add(new LwPolylineVertex(v15));
            line4.Vertexes.Add(new LwPolylineVertex(v18));
            line4.Vertexes.Add(new LwPolylineVertex(v19));
            line4.Vertexes.Add(new LwPolylineVertex(v17));
            line4.Vertexes.Add(new LwPolylineVertex(v13));
            line4.Vertexes[2].Bulge = Math.Tan(73 * pi / 180 / 4);
            fun.PolyFill(dxf, line4, 0.3, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v15));
            line5.Vertexes.Add(new LwPolylineVertex(v16));
            line5.Vertexes.Add(new LwPolylineVertex(v4));
            fun.PolyFill(dxf, line5, 0.3, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v5));
            line6.Vertexes.Add(new LwPolylineVertex(v6));
            line6.Vertexes.Add(new LwPolylineVertex(v7));
            line6.Vertexes.Add(new LwPolylineVertex(v8));
            fun.PolyFill(dxf, line6, 0.3, contour);

            LwPolyline line7 = new LwPolyline();
            line7.Vertexes.Add(new LwPolylineVertex(v9));
            line7.Vertexes.Add(new LwPolylineVertex(v2));
            line7.Vertexes.Add(new LwPolylineVertex(v3));
            line7.Vertexes.Add(new LwPolylineVertex(v11));
            fun.PolyFill(dxf, line7, 0.3, contour);

            LwPolyline line8 = new LwPolyline();
            line8.Vertexes.Add(new LwPolylineVertex(v22));
            line8.Vertexes.Add(new LwPolylineVertex(v23));
            line8.Vertexes[0].Bulge = Math.Tan(-94 * pi / 180 / 4);
            fun.PolyFill(dxf, line8, 0.3, contour);

            LwPolyline line9 = new LwPolyline();
            line9.Vertexes.Add(new LwPolylineVertex(v10));
            line9.Vertexes.Add(new LwPolylineVertex(v6));
            line9.Vertexes.Add(new LwPolylineVertex(v7));
            line9.Vertexes.Add(new LwPolylineVertex(v12));
            fun.PolyFill(dxf, line9, 0.3, contour);

            Line line10 = new Line(v31, v32);
            line10.Layer = centre;
            line10.Linetype = Linetype.DashDot;
            line10.LinetypeScale = 3;
            dxf.AddEntity(line10);

            Line line11 = new Line(v29, v30);
            line11.Layer = centre;
            line11.Linetype = Linetype.DashDot;
            line11.LinetypeScale = 3;
            dxf.AddEntity(line11);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v42));
            line12.Vertexes.Add(new LwPolylineVertex(v33));
            line12.Vertexes.Add(new LwPolylineVertex(v34));
            line12.Vertexes.Add(new LwPolylineVertex(v38));
            line12.Vertexes.Add(new LwPolylineVertex(v39));
            line12.Vertexes.Add(new LwPolylineVertex(v49));
            fun.PolyFill(dxf, line12, 0.3, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v43));
            line14.Vertexes.Add(new LwPolylineVertex(v44));
            line14.Vertexes.Add(new LwPolylineVertex(v45));
            line14.Vertexes.Add(new LwPolylineVertex(v40));
            line14.Vertexes.Add(new LwPolylineVertex(v41));
            line14.Vertexes.Add(new LwPolylineVertex(v42));
            line14.Vertexes.Add(new LwPolylineVertex(v43));
            line14.Vertexes[4].Bulge = Math.Tan(-128 * pi / 180 / 4);
            fun.PolyFill(dxf, line14, 0.3, contour);

            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v46));
            line15.Vertexes.Add(new LwPolylineVertex(v47));
            line15.Vertexes.Add(new LwPolylineVertex(v48));
            line15.Vertexes.Add(new LwPolylineVertex(v50));
            line15.Vertexes.Add(new LwPolylineVertex(v51));
            line15.Vertexes[3].Bulge = Math.Tan(-77 * pi / 180 / 4);
            fun.PolyFill(dxf, line15, 0.3, contour);

            LwPolyline line16 = new LwPolyline();
            line16.Vertexes.Add(new LwPolylineVertex(v52));
            line16.Vertexes.Add(new LwPolylineVertex(v53));
            line16.Vertexes[0].Bulge = Math.Tan(-137 * pi / 180 / 4);
            fun.PolyFill(dxf, line16, 0.3, contour);


            //标注

            double height = 2;
            double arrowsize = 1;
            double exline = 2;




            fun.dimrot1(dxf, v6, v7, -90, b, 1, height, arrowsize, exline, "b=" + b, "", "");

            fun.dimrot1(dxf, v16, v13, -90, b * 2, 1, height, arrowsize, exline, "n=" + n, "", "");

            fun.dimrot1(dxf, v13, v3, 180, b*1.7, 1, height, arrowsize, exline, "l2=" + l2, "", "");

            fun.dimrot1(dxf, v13, v6, 180, b * 2.5, 1, height, arrowsize, exline, "l1=" + l1, "", "");

            fun.dimrot1(dxf, v13, v14, -175, b, 1, height, arrowsize, exline, "l=" + l, "", "");

            fun.dimrot1(dxf, v2, v3, 90, b, 1, height, arrowsize, exline, "d=" + d, "", "");

            fun.dimrot1(dxf, v38, v35, 0, b * 3 / 2, 1, height, arrowsize, exline, "h=" + h, "", "");

          //  fun.dimrot1(dxf, v40, v35, -90, b * 2, 1, height, arrowsize, exline, "D=" + D, "", "");

            fun.jdbz1(dxf, new Line(v13, fun.Polar(v13, 85 * pi / 180, 0.1)), new Line(v13, fun.Polar(v13, 180 * pi / 180, 0.1)), 14, "95°", 1);



            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.SetLayer(dxf);

            fun.SetByLayer(dxf);

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}

